package org.jsola.hr.web;

import org.apache.commons.io.FilenameUtils;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.common.ExcelKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.EmpSocBackRecordInfoAddDTO;
import org.jsola.hr.dto.EmpSocBackRecordInfoUpdateDTO;
import org.jsola.hr.query.EmpSocBackRecordInfoQuery;
import org.jsola.hr.service.IEmpSocBackRecordInfoService;
import org.jsola.hr.vo.EmpSocBackRecordInfoListVO;
import org.jsola.hr.vo.EmpSocBackRecordInfoVO;
import org.jsola.hr.vo.excel.AttendPunchRecordExportVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.nio.charset.StandardCharsets;
import java.util.List;


/**
 * 社保回款信息
 *
 * @author zhr
 */
@RestController("hrEmpSocBackRecordInfoController")
@RequestMapping(HrConstants.API_PATH + "/v1/empSocBackRecordInfos")
public class EmpSocBackRecordInfoController implements IEmpSocBackRecordInfoControllerAPI {

    @Autowired
    private IEmpSocBackRecordInfoService empSocBackRecordInfoService;

    @Override
    @PostMapping
    public Result<EmpSocBackRecordInfoVO> save(@Valid @RequestBody EmpSocBackRecordInfoAddDTO empSocBackRecordInfoAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增社保回款信息权限校验

        return Result.success(
                empSocBackRecordInfoService.save(empSocBackRecordInfoAddDTO, tokenUser)
        );
    }

    @Override
    @PostMapping("/saveBatch")
    public Result<Integer> saveBatch(@RequestBody EmpSocBackRecordInfoAddDTO[] empSocBackRecordInfos) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增社保回款信息权限校验

        return Result.success(
                empSocBackRecordInfoService.saveBatch(empSocBackRecordInfos, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody EmpSocBackRecordInfoUpdateDTO empSocBackRecordInfoUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改社保回款信息权限校验

        int count = empSocBackRecordInfoService.update(empSocBackRecordInfoUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @DeleteMapping(value = "/{empSocBackRecordInfoId}")
    public Result<Integer> delete(@PathVariable String empSocBackRecordInfoId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除社保回款信息权限校验

        int count = empSocBackRecordInfoService.deleteByIds(tokenUser, empSocBackRecordInfoId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] empSocBackRecordInfoIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除社保回款信息权限校验

        int count = empSocBackRecordInfoService.deleteByIds(tokenUser, empSocBackRecordInfoIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{empSocBackRecordInfoId}")
    public Result<EmpSocBackRecordInfoVO> findById(@PathVariable String empSocBackRecordInfoId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看社保回款信息权限校验

        return Result.success(
                empSocBackRecordInfoService.selectById(empSocBackRecordInfoId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<EmpSocBackRecordInfoListVO>> listAll(@Valid EmpSocBackRecordInfoQuery empSocBackRecordInfoQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看社保回款信息权限校验

        List<EmpSocBackRecordInfoListVO> empSocBackRecordInfoListVOList = empSocBackRecordInfoService
                .select(empSocBackRecordInfoQuery, tokenUser.getSiteId());
        empSocBackRecordInfoListVOList.forEach(empSocBackRecordInfoListVO ->
                empSocBackRecordInfoService.setEmpInfoToEmpSocBackRecordInfo(empSocBackRecordInfoListVO, tokenUser));
        return Result.success(empSocBackRecordInfoListVOList);
    }


    @Override
    @GetMapping
    public Result<Page<EmpSocBackRecordInfoListVO>> page(@Valid EmpSocBackRecordInfoQuery empSocBackRecordInfoQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        Page<EmpSocBackRecordInfoListVO> page = empSocBackRecordInfoService
                .selectPage(empSocBackRecordInfoQuery, tokenUser.getSiteId());
        page.getEntities().forEach(empSocBackRecordInfoListVO ->
                empSocBackRecordInfoService.setEmpInfoToEmpSocBackRecordInfo(empSocBackRecordInfoListVO, tokenUser));
        return Result.success(page);
    }

    @Override
    @PostMapping("/pagePost")
    public Result<Page<EmpSocBackRecordInfoListVO>> pagePost(@RequestBody @Valid EmpSocBackRecordInfoQuery empSocBackRecordInfoQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                empSocBackRecordInfoService.selectPage(empSocBackRecordInfoQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @PostMapping("/exportRecordInfo")
    public ResponseEntity<Resource> exportRecordInfo(@RequestBody @Valid EmpSocBackRecordInfoQuery query) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 导出权限校验

        // 导出路径
        String excelFilePath = empSocBackRecordInfoService.selectExportPath(query, tokenUser.getSiteId());
        // 编码文件名，防止乱码
        String filenames = new String(FilenameUtils.getName(excelFilePath).getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=\"" + filenames + "\"")
                .header(HttpHeaders.CONTENT_TYPE, "application/vnd.ms-excel")
                .body(new FileSystemResource(excelFilePath));
    }


}

